package com.example.common.widget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.example.common.R
import com.example.common.base.BaseDialog
import com.example.common.tools.TooldUtils
import com.example.common.tools.isNotEmpty


class DownloadDialog : BaseDialog {


    private var mContext: Context? = null

    private var mview: View? = null

    private var botton: TextView? = null

    private var bt_cancel: TextView? = null

    private var progress: DownProgress? = null

    private var iv_close: ImageView? = null

    constructor(context: Context) : super(context) {
        this.mContext = context
        initView()
    }



    constructor(context: Context, themeResId: Int) : super(context, themeResId) {
        this.mContext = context
        initView()
    }


    override fun initView() {
        mview = LayoutInflater.from(context)
            .inflate(R.layout.dialog_layout, null)
        botton = mview!!.findViewById(R.id.bt_ok)
        bt_cancel = mview!!.findViewById(R.id.bt_cancel)
        progress = mview!!.findViewById(R.id.progress)
        iv_close = mview!!.findViewById(R.id.iv_close)
        iv_close!!.setOnClickListener {
            dismiss()
        }
    }

    override fun layout()= mview!!

    fun setTitle(title: String): DownloadDialog {
        if (title.isNotEmpty())
            mview?.findViewById<TextView>(R.id.tv_title)?.text = title
        return this
    }

    fun setMessage(msg: String): DownloadDialog {
        if (msg.isNotEmpty())
            mview?.findViewById<TextView>(R.id.tv_body)?.text = msg
        return this
    }

    fun setBotton(listener: View.OnClickListener): DownloadDialog {
        botton?.setOnClickListener(listener)
        return this
    }

    fun setCancel(listener: View.OnClickListener): DownloadDialog {
        bt_cancel?.setOnClickListener(listener)
        return this
    }

    fun hideButton() {
        progress?.visibility = View.VISIBLE
        botton?.visibility = View.GONE
        bt_cancel?.visibility = View.GONE
    }

    fun getCancel(): TextView {
        return bt_cancel!!
    }

    fun getProgress(): DownProgress {
        return progress!!
    }

    override fun show() {
        super.show()
//        val layoutParams = window!!.attributes
//        layoutParams.gravity = Gravity.CENTER
//        layoutParams.flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//                or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
//                or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
//        layoutParams.gravity = Gravity.CENTER
//        layoutParams.format = PixelFormat.TRANSLUCENT
//        layoutParams.width = DensityUtil.dip2px(context, 300f).toInt()
//        layoutParams.height = DensityUtil.dip2px(context, 250f).toInt()
//        window!!.attributes = layoutParams
    }
}

